﻿using System.Globalization;
using Aptea.OpenXmlOffice;
using eCRAM.Bll.Properties;
using eCRAM.Model;
using eCRAM.Tools;
using DocumentFormat.OpenXml.Spreadsheet;

namespace eCRAM.Bll
{
    /// <summary>
    /// Classe générant l'en-tête de l'onglet contenant le tableau de détails des imputations d'un cram
    /// </summary>
    public class ExcelSheetHeaderGenerator
    {
        /// <summary>
        /// Instance de manager Excel
        /// </summary>
        public ExcelManager ExcelManager { get; set; }
        /// <summary>
        /// Index de la ligne en cours d'insertion dans excel
        /// </summary>
        public uint IdxRow { get; set; }
        /// <summary>
        /// Instance de l'onglet
        /// </summary>
        public Worksheet WorkSheet { get; set; }
        /// <summary>
        /// Cram contenant les données à exporter
        /// </summary>
        public Cram Cram { get; set; }

        /// <summary>
        /// Insere l'en-tête
        /// </summary>
        /// <returns></returns>
        public uint Generate()
        {
            IdxRow = AddTitle();
            IdxRow = AddCramIdentity();

            return IdxRow;
        }

        /// <summary>
        /// Ajout du titre et du logo
        /// </summary>
        /// <returns></returns>
        private uint AddTitle()
        {
            ExcelManager.AddImageToSheet(WorkSheet, 1, 2, new ImageExcelInformation(Resources.LogoAPTEA) { Width = 120, Height = 71, MarginLeft = 10, MarginTop = 10 });
            WorkSheet.GetMergeCells(IdxRow, 4, IdxRow, 30);
            ExcelManager.AddDataToSheet(WorkSheet, IdxRow, 4,  "COMPTE-RENDU D'ACTIVITE MENSUELLE (CRAM)");
            ExcelManager.AddStyleToCells(WorkSheet, IdxRow, 4, IdxRow, 30, CramExcelStyle.TitleStyle);
           
            IdxRow++;

            return IdxRow;
        }

        /// <summary>
        /// Ajout de la section contenant les détails sur le cram
        /// </summary>
        /// <returns></returns>
        private uint AddCramIdentity()
        {
            const uint idxColIdentity = 2;

            // Merge
            //WorkSheet.GetMergeCells(IdxRow, idxColIdentity, IdxRow, 5);

            // MOIS / ANNEE
            var cell = ExcelManager.AddDataToSheet(WorkSheet, IdxRow, idxColIdentity,
                string.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[Cram.Month - 1].Capitalize(), Cram.Year));
            ExcelManager.AddStyleToCell(cell, CramExcelStyle.DefaultTopBorderStyle);
            
            // NOM / PRENOM
            IdxRow++;
            //WorkSheet.GetMergeCells(IdxRow, idxColIdentity, IdxRow, 5);
            cell = ExcelManager.AddDataToSheet(WorkSheet, IdxRow, idxColIdentity, string.Format("{0} {1}", Cram.User.FirstName, Cram.User.LastName));
            ExcelManager.AddStyleToCell(cell ,CramExcelStyle.DefaultBottomCellBorderStyle);
            IdxRow++;
            
            // SAUT DE LIGNE
            ExcelManager.AddDataToSheet(WorkSheet, IdxRow, idxColIdentity, "");

            return IdxRow;
        }
    }
}
